<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>DWR测试</title>
</head>
<script type='text/javascript' src='<%=basePath%>dwr/engine.js'></script>  
<script type='text/javascript' src='<%=basePath%>dwr/util.js'></script> 
<script type='text/javascript' src='<%=basePath%>dwr/interface/MyDWR.js'></script>   
<script type='text/javascript'>
	window.onload = function() {
		
		
 		document.getElementById("button").onclick = function() {
 			
 			var a = document.getElementById("num1").value;
 			var b = document.getElementById("num2").value;
 			if(a==null||a==""||b==null||b=="") {
 				alert("a值b值不能为空");
 				return;
 			}
 			//方法1：callback 利用回调函数获取java端的返回值
 	       /* 			
 	        MyDWR.add(a, b, {
				callback : function(str) {
					//alert(str);
					document.getElementById("num3").value = str;
					document.getElementById("showok").innerHTML = "计算成功"; 
				}
			});
 			 */
 			//方法2：出现超时处理时，对延迟进行处理
            /*  			
            MyDWR.add(a, b, {
 				  callback:function(str) { 
 					 document.getElementById("num3").value = str;
 					document.getElementById("showok").innerHTML = "计算成功"; 
 				  },
 				  timeout:5000,
 				  errorHandler:function(message) { alert("Oops: " + message); }
 				});
 			  */
 			  
 			//方法三 :对方法一进行分步处理，效果一样 
 			MyDWR.add(a, b, returnresult);  
 			function returnresult(str) {
 				 document.getElementById("num3").value = str;
				 document.getElementById("showok").innerHTML = "计算成功！"; 
 				}
  
		}
		
		
		
	};
</script>  
<body>

<input type="text" value="" id="num1" name="num1" />
<br/>
+
<br/>
<input type="text" value="" id="num2" name="num2" />
<br/>
=
<br/>
<input type="text" value="" id="num3" name="num3" />
<input type="button" value="计算一下" id="button" name="button" />
<div id="showok">
</div>
</body>
</html>